package com.xiaoyu.izf.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.xiaoyu.izf.annotation.RequestAuth;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.service.UserInfoService;
import com.xiaoyu.izf.service.YqfStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Controller
@RequestMapping("/home")
public class HomeController extends BaseController{

    @Autowired
    private YqfStatsService yqfStatsService;
    @Autowired
    private UserInfoService userInfoService;
    private static ExecutorService service = Executors.newFixedThreadPool(30);

    @RequestMapping("/index")
    @ResponseBody
    @RequestAuth
    public String index(HttpServletRequest request, Model model)throws Exception{
        String initiald = "1970-01-01";
        String yesd = DateUtil.yesterday().toSqlDate().toString();
        UserInfo user = user(request);
//        UserInfo user = userInfoService.findByUserName("15208344647");

//        double oaYesd;//昨日订单总额
//        oaYesd = yqfStatsService.ordersAmount(user, yesd, yesd);
//
//        double oaUntilYesd;//截止昨日订单总额
//        oaUntilYesd = yqfStatsService.ordersAmount(user, initiald,yesd);
//
//        int ordersYesd;//昨日订单总数
//        ordersYesd = yqfStatsService.ordersCount(user, yesd,yesd);
//        int ordersUntilYesd;//截止昨日订单总数
//        ordersUntilYesd = yqfStatsService.ordersCount(user, initiald,yesd);
//
//        double scYesd;//昨日流水返佣
//        scYesd = yqfStatsService.serialCommission(user,yesd,yesd);
//        double scUntilYesd;//截至昨日流水返佣
//        scUntilYesd = yqfStatsService.serialCommission(user,initiald,yesd);

        double oaYesd;//昨日订单总额
        Future<Double> oaYesdFuture = service.submit(new Callable<Double>() {
            @Override
            public Double call() throws Exception {
                return yqfStatsService.ordersAmount(user, yesd, yesd);
            }
        });

        double oaUntilYesd;//截止昨日订单总额
        Future<Double> oaUntilYesdFuture = service.submit(new Callable<Double>() {
            @Override
            public Double call() throws Exception {
                return yqfStatsService.ordersAmount(user, initiald,yesd);
            }
        });

        int ordersYesd;//昨日订单总数
        Future<Integer> ordersYesdFuture = service.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return yqfStatsService.ordersCount(user, yesd,yesd);
            }
        });

        int ordersUntilYesd;//截止昨日订单总数
        Future<Integer> ordersUntilYesdFuture = service.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                return yqfStatsService.ordersCount(user, initiald,yesd);
            }
        });

        double scYesd;//昨日流水返佣
        Future<Double> scYesdFuture = service.submit(new Callable<Double>() {
            @Override
            public Double call() throws Exception {
                return yqfStatsService.serialCommission(user,yesd,yesd);
            }
        });

        double scUntilYesd;//截至昨日流水返佣
        Future<Double> scUntilYesdFuture = service.submit(new Callable<Double>() {
            @Override
            public Double call() throws Exception {
                return yqfStatsService.serialCommission(user,initiald,yesd);
            }
        });

        oaYesd = oaYesdFuture.get();
        oaUntilYesd = oaUntilYesdFuture.get();
        ordersYesd = ordersYesdFuture.get();
        ordersUntilYesd = ordersUntilYesdFuture.get();
        scYesd = scYesdFuture.get();
        scUntilYesd = scUntilYesdFuture.get();

        Map<String,Object> map = new HashMap<>();
        map.put("oaYesd",oaYesd);
        map.put("oaUntilYesd",oaUntilYesd);
        map.put("ordersYesd",ordersYesd);
        map.put("ordersUntilYesd",ordersUntilYesd);
        map.put("scYesd",scYesd);
        map.put("scUntilYesd",scUntilYesd);
        model.addAttribute("stsData",map);

        return JSONUtil.toJsonStr(model);
    }
}
